package com.bigflexlib.x.flash.events
{
	
	import flash.events.IEventDispatcher;
	
	
	/**
	 * 
	 * A self contained VO to pass around the subscription information for an event.
	 * Most flex framework methods take a simple ordered tuple of arguments, but we do some
	 * complex stuff with this structure so it makes more sense to pack it all into something more manageable.
	 * 
	 * It also provides utility methods to add/remove the specified listener from one or more dispatchers.
	 * 
	 * @author aldo.bucchi@gmail.com
	 * @see com.bigflexlib.ds.container.utils.ContainerElementsListenerHelper
	 * 
	 */	
	public class EventListenerSpec
	{



		public var type:String;
		
		public var listener:Function;
		
		public var useCapture:Boolean;
		
		public var priority:int;
		
		public var useWeakReference:Boolean;



		
		public function EventListenerSpec( type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false )
		{
		
			
			this.type = type;
			
			this.listener = listener;
			
			this.useCapture = useCapture;
			
			this.priority = priority;
			
			this.useWeakReference = useWeakReference;
			
		}
		
		
		
		
		public function addToDispatcher( dispatcher:IEventDispatcher ):void
		{
			dispatcher.addEventListener( type, listener, useCapture, priority, useWeakReference );
		}



		
		public function removeFromDispatcher( dispatcher:IEventDispatcher ):void
		{
			dispatcher.removeEventListener( type, listener, useCapture );		
		}
		



		
		public function addToDispatcherUsingProxy( dispatcher:IEventDispatcher ):EventListenerProxy
		{
			return new EventListenerProxy( this, dispatcher );
		}




		
		
		/**
		 * 
		 * Bulk addition to many IEventDispatchers.
		 * If one of the items in the array fails to implement the IEventDispatcher interface
		 * this method will simply skip it and continue iterating.
		 * 
		 * @param dispatchers
		 * 
		 */		
		public function addToMany( dispatchers:Array ):void
		{
			for each ( var e:* in dispatchers )
				if ( e is IEventDispatcher )
					addToDispatcher( e as IEventDispatcher );
		}
		


		
		/**
		 * 
		 * Bulk removal from many IEventDispatchers.
		 * If one of the items in the array fails to implement the IEventDispatcher interface
		 * this method will simply skip it and continue iterating.
		 * 
		 * @param dispatchers
		 * 
		 */		
		public function removeFromMany( dispatchers:Array ):void
		{
			for each ( var e:* in dispatchers )
				if ( e is IEventDispatcher )
					removeFromDispatcher( e as IEventDispatcher );
		}		
		
		


	}

}